﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Home {

    public class LoginModel : ApplicationModel {
        
        #region Private Properties

        private Security.AuthenticationResponse authenticationResponse = new Security.AuthenticationResponse ();

        private List<Int64> environmentRestrictions = null;

        #endregion


        #region Public Properties

        public Security.AuthenticationResponse AuthenticationResponse { get { return authenticationResponse; } set { authenticationResponse = value; } }

        public List<Int64> EnvironmentRestrictions {

            get {

                if (environmentRestrictions == null) {

                    environmentRestrictions = new List<Int64> ();


                    // READ IN THE RESTRICTED ENVIRONMENT IDS FROM THE .CONFIG FILE, CONVERT TO INTEGERS AND COMPILE LIST

                    String[] environmentRestrictionsConfig = System.Configuration.ConfigurationManager.AppSettings.GetValues ("EnvironmentRestrictions");

                    if (environmentRestrictionsConfig == null) { environmentRestrictionsConfig = new String[0]; }


                    foreach (String currentRestrictedEnvironmentId in String.Join ("|", environmentRestrictionsConfig).Split ('|')) {

                        Int64 restrictedEnvironmentId = 0;

                        if (Int64.TryParse (currentRestrictedEnvironmentId, out restrictedEnvironmentId)) {

                            if ((restrictedEnvironmentId != 0) && (!environmentRestrictions.Contains (restrictedEnvironmentId))) {

                                environmentRestrictions.Add (restrictedEnvironmentId);

                            }

                        }

                    }

                }

                return environmentRestrictions;

            }

        }

        public String ThemeDefault {

            get {

                String themeName = "Redmond";

                String[] themeDefaultConfig = System.Configuration.ConfigurationManager.AppSettings.GetValues ("ThemeDefault");

                if (themeDefaultConfig == null) { themeDefaultConfig = new String[0]; }

                if (themeDefaultConfig.Length == 1) {

                    // VALIDATE THAT THE THEME EXISTS ON THE SERVER BEFORE USING, OTHERWISE DEFAULT TO REDMOND 

                    if (System.IO.File.Exists (System.Web.Hosting.HostingEnvironment.MapPath ("~/Assets/Css/Themes/" + themeDefaultConfig[0].Trim () + "/jquery-ui-1.10.2.min.css"))) {

                        themeName = themeDefaultConfig[0].Trim ();

                    }
                
                }

                return themeName;

            }

        }

        public System.Web.Mvc.SelectList EnvironmentSelectList {

            get {

                List<Enterprise.Environment> items = new List<Enterprise.Environment> ();

                if (authenticationResponse != null) {

                    items = (from currentEnvironment in Application.Enterprise.EnvironmentsByUserAccountId (authenticationResponse.UserAccountId)

                             orderby currentEnvironment.Name

                             select currentEnvironment).ToList ();

                }

                return new System.Web.Mvc.SelectList (items, "Id", "Name");

            }

        }

        #endregion 


        #region Constructors

        public LoginModel () {

            return;

        }

        public LoginModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);

           return;

        }

        public void Authenticate (Int64 environmentId) {

            authenticationResponse = Application.Authenticate (environmentId, EnvironmentRestrictions);

            return;

        }

        #endregion 

    }

}